#include "DIALOG.h"
#include "MC_include.h"
#include "Maintask.h"

#define ID_FRAMEWIN_0     (GUI_ID_USER + 0x20)

#define ID_RTEXT_0  (GUI_ID_USER + 0x101)
#define ID_REDIT_0  (GUI_ID_USER + 0x102)
#define ID_RTEXT_1  (GUI_ID_USER + 0x103)
#define ID_RTEXT_2  (GUI_ID_USER + 0x104)
#define ID_RTEXT_3  (GUI_ID_USER + 0x105)
#define ID_RTEXT_4  (GUI_ID_USER + 0x106)
#define ID_RTEXT_5  (GUI_ID_USER + 0x107)
#define ID_RTEXT_6  (GUI_ID_USER + 0x108)
#define ID_RBUTTON_0  (GUI_ID_USER + 0x109)
#define ID_RBUTTON_1  (GUI_ID_USER + 0x10A)
#define ID_RBUTTON_2  (GUI_ID_USER + 0x10B)
#define ID_RBUTTON_3  (GUI_ID_USER + 0x10C)
#define ID_RBUTTON_4  (GUI_ID_USER + 0x10D)
#define ID_RBUTTON_5  (GUI_ID_USER + 0x10E)
#define ID_RBUTTON_6  (GUI_ID_USER + 0x10F)
#define ID_RBUTTON_7  (GUI_ID_USER + 0x110)
#define ID_RBUTTON_8  (GUI_ID_USER + 0x111)
#define ID_RBUTTON_9  (GUI_ID_USER + 0x112)
#define ID_RBUTTON_10  (GUI_ID_USER + 0x113)
#define ID_RBUTTON_11  (GUI_ID_USER + 0x114)
#define ID_RBUTTON_12  (GUI_ID_USER + 0x115)
#define ID_RBUTTON_13  (GUI_ID_USER + 0x116)
#define ID_RBUTTON_14  (GUI_ID_USER + 0x117)
#define ID_RBUTTON_15  (GUI_ID_USER + 0x118)
#define ID_RBUTTON_16  (GUI_ID_USER + 0x119)
#define ID_RBUTTON_17  (GUI_ID_USER + 0x11A)
#define ID_RBUTTON_18  (GUI_ID_USER + 0x11B)
#define ID_RBUTTON_19  (GUI_ID_USER + 0x11C)
#define ID_RBUTTON_20  (GUI_ID_USER + 0x11D)
#define ID_RBUTTON_21  (GUI_ID_USER + 0x11E)
#define ID_RBUTTON_22  (GUI_ID_USER + 0x11F)
#define ID_RBUTTON_23  (GUI_ID_USER + 0x120)
#define ID_RBUTTON_24  (GUI_ID_USER + 0x121)
#define ID_RBUTTON_25  (GUI_ID_USER + 0x122)
#define ID_RBUTTON_26  (GUI_ID_USER + 0x123)
#define ID_RBUTTON_27  (GUI_ID_USER + 0x124)
#define ID_RBUTTON_28  (GUI_ID_USER + 0x125)
#define ID_RBUTTON_29  (GUI_ID_USER + 0x126)
#define ID_RBUTTON_30  (GUI_ID_USER + 0x127)
#define ID_RBUTTON_31  (GUI_ID_USER + 0x128)
#define ID_RBUTTON_32  (GUI_ID_USER + 0x129)
#define ID_RTEXT_7  (GUI_ID_USER + 0x12A)


extern uint8_t runWin_flag;
uint8_t R_Pressed = 0;
extern uint8_t cicr_ex;
WM_HWIN runWin;
uint8_t runMode_flag = 0; //0 cmd mode 1 program mode
uint8_t IsOpenFile = 0;
uint8_t executePragram = 0;
extern uint8_t open_edit_flag;
extern char celfile_text_buffer[11];
/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

static const GUI_WIDGET_CREATE_INFO _aDialogRunwinCreate[] =
{
    { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_0, 0, 0, 800, 480, 0, 0x64, 0 },
    { TEXT_CreateIndirect, "axis_info", ID_RTEXT_0, 0, 10, 495, 180, 0, 0x64, 0 },
    { EDIT_CreateIndirect, "Edit", ID_REDIT_0, 100, 195, 325, 40, 0, 0x1e, 0 },
    { TEXT_CreateIndirect, " ", ID_RTEXT_1, 12, 197, 83, 35, 0, 0x64, 0 },
    { TEXT_CreateIndirect, " ", ID_RTEXT_2, 100, 275, 295, 30, 0, 0x64, 0 },
    { TEXT_CreateIndirect, " ", ID_RTEXT_3, 100, 305, 295, 30, 0, 0x0, 0 }, //正在执行程序
    { TEXT_CreateIndirect, " ", ID_RTEXT_4, 100, 335, 295, 30, 0, 0x0, 0 },
    { TEXT_CreateIndirect, " ", ID_RTEXT_5, 100, 365, 295, 30, 0, 0x64, 0 },
    { TEXT_CreateIndirect, " ", ID_RTEXT_6, 100, 395, 295, 30, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Open", ID_RBUTTON_0, 507, 56, 83, 29, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Mode", ID_RBUTTON_1, 608, 56, 82, 30, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "G", ID_RBUTTON_2, 505, 100, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "M", ID_RBUTTON_3, 555, 100, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "S", ID_RBUTTON_4, 605, 100, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "T", ID_RBUTTON_5, 655, 100, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "1", ID_RBUTTON_6, 555, 239, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "2", ID_RBUTTON_7, 605, 239, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "3", ID_RBUTTON_8, 655, 239, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "BAK", ID_RBUTTON_9, 705, 239, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "4", ID_RBUTTON_10, 555, 285, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "5", ID_RBUTTON_11, 605, 285, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "6", ID_RBUTTON_12, 655, 285, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Del", ID_RBUTTON_30, 705, 285, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "7", ID_RBUTTON_13, 555, 332, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "8", ID_RBUTTON_14, 605, 332, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "9", ID_RBUTTON_15, 655, 332, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Enter", ID_RBUTTON_31, 705, 332, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "-", ID_RBUTTON_16, 555, 380, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "0", ID_RBUTTON_17, 605, 380, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, ".", ID_RBUTTON_18, 655, 380, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Cancel", ID_RBUTTON_32, 705, 380, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "R", ID_RBUTTON_19, 705, 100, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "X", ID_RBUTTON_20, 505, 146, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Y", ID_RBUTTON_21, 555, 146, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Z", ID_RBUTTON_22, 605, 146, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "I", ID_RBUTTON_23, 655, 146, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "P", ID_RBUTTON_24, 705, 146, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "F", ID_RBUTTON_25, 705, 191, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "J", ID_RBUTTON_26, 655, 191, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "W", ID_RBUTTON_27, 605, 191, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "V", ID_RBUTTON_28, 555, 191, 40, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "U", ID_RBUTTON_29, 505, 190, 40, 40, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "EX_info", ID_RTEXT_7, 509, 12, 239, 40, 0, 0x0, 0 },
};

static void _cbRunwinDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    int     NCode;
    int     Id;
    R_Pressed = 0;
	char edit_buf[21];
    switch (pMsg->MsgId)
    {
        case WM_DELETE:
            runWin_flag = 0;
            break;
	
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Framewin'
            //
            hItem = pMsg->hWin;
            FRAMEWIN_SetSkinClassic(hItem);
            FRAMEWIN_SetActive(hItem, 1);
            FRAMEWIN_SetTitleHeight(hItem, 32);
            FRAMEWIN_SetFont(hItem, &GUI_Fontfont20B);
            FRAMEWIN_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            FRAMEWIN_SetText(hItem, "运行");
            FRAMEWIN_AddCloseButton(hItem, FRAMEWIN_BUTTON_RIGHT, 0);
            FRAMEWIN_SetClientColor(hItem, GUI_MAKE_COLOR(0x00E7E7E7));

            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_0);
            TEXT_SetFont(hItem, &GUI_Fontfont32B);
			TEXT_SetText(hItem, "绝对坐标\n X:0.0mm  Y:0.0mm  Z:0.0mm\nxiandui\n X:0.0mm  Y:0.0mm  Z:0.0mm\n  C:000 F:0");
            //
            // Initialization of 'Edit'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_REDIT_0);
            EDIT_SetText(hItem, "");
            EDIT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            EDIT_SetFont(hItem, GUI_FONT_24B_1);
            //
            // Initialization of 'Text'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_1);
            TEXT_SetTextAlign(hItem, GUI_TA_RIGHT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_24B_1);
            TEXT_SetText(hItem, "CMD >");
            //
            // Initialization of 'PROGRAM1'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_2);
            TEXT_SetText(hItem, " ");
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20B_1);
			TEXT_SetBkColor(hItem,GUI_WHITE);
            //
            // Initialization of 'PROGRAM2'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_3);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20B_1);
			TEXT_SetBkColor(hItem,GUI_GREEN);
            //
            // Initialization of 'PROGAM3'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_4);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20B_1);
			TEXT_SetBkColor(hItem,GUI_WHITE);
            //
            // Initialization of 'PROGAM4'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_5);
            TEXT_SetText(hItem, "");
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20B_1);
			TEXT_SetBkColor(hItem,GUI_WHITE);
            //
            // Initialization of 'PROGRAM5'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_6);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20B_1);
			TEXT_SetBkColor(hItem,GUI_WHITE);

            //按键初始化
            for(int i = ID_RBUTTON_2; i <= ID_RBUTTON_29; i ++)
            {
                hItem = WM_GetDialogItem(pMsg->hWin, i);
                BUTTON_SetFocussable(hItem, 0);  //按键失去聚焦
                BUTTON_SetFont(hItem, GUI_FONT_16B_1);
            }

            //
            // Initialization of 'EX_info'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_RTEXT_7);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetTextColor(hItem, GUI_MAKE_COLOR(0x00000000));
            TEXT_SetFont(hItem, &GUI_Fontfont20B);
            break;

        case WM_NOTIFY_PARENT:
            Id    = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            if(Id >= ID_RBUTTON_2 && Id <= ID_RBUTTON_29)
            {
                switch(NCode)
                {
                    case WM_NOTIFICATION_CLICKED:
                        R_Pressed = 1;

                    case WM_NOTIFICATION_RELEASED:
                    {
                        int Key;

                        if(Id != ID_RBUTTON_9)
                        {
                            char acBuffer[20];
                            BUTTON_GetText(pMsg->hWinSrc, acBuffer, sizeof(acBuffer)); /* Get the text of the button */
                            Key = acBuffer[0];
                        }
                        else
                        {
                            Key = GUI_KEY_BACKSPACE;
                        }

                        GUI_SendKeyMsg(Key, R_Pressed); /*Send a key message to the focussed window */
                    }
                    break;

                    default:
                        WM_DefaultProc(pMsg);
                }
            }

            switch(Id)
            {
                case ID_RBUTTON_31:
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            break;

                        case WM_NOTIFICATION_RELEASED:
							if(runMode_flag == 0)
							{
								hItem = WM_GetDialogItem(pMsg->hWin, ID_REDIT_0);
								EDIT_GetText(hItem, edit_buf, 20);
								gc_init();
								gc_execute_line(edit_buf);							
							}
							else if(runMode_flag == 1)
							{
								TEXT_SetBkColor(WM_GetDialogItem(pMsg->hWin, ID_RTEXT_3),GUI_GREEN);
								executePragram = 1;
							}
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }
					break;
				case ID_RBUTTON_0:
					switch(NCode)
					{
						case WM_NOTIFICATION_CLICKED: break;
						case WM_NOTIFICATION_RELEASED:
							CreateFilewin(runWin);
							break;
					}						
				break;
				case ID_RBUTTON_1:
					switch(NCode)
					{
						case WM_NOTIFICATION_CLICKED: break;
						case WM_NOTIFICATION_RELEASED:
							if(runMode_flag == 0)
							{
								runMode_flag = 1;
								printf("program mode");
							}
							else if(runMode_flag == 1)
							{
								runMode_flag = 0;
								printf("cmd mode");
							}
							break;
					}						
				break;
				default:
                    WM_DefaultProc(pMsg);
            }

            break;

        default:
            WM_DefaultProc(pMsg);
            break;
    }
}

FIL exe_text;
FRESULT exe_text_res;
UINT exe_text_fnum;
WM_HWIN CreateRunwin(void);
WM_HWIN CreateRunwin(void)
{
    WM_HWIN hWin;
	WM_HWIN hItem;
	char textbuff[50];
	char read_buffer[1024] ={0};
	char ex_text_buff[50];
    hWin = GUI_CreateDialogBox(_aDialogRunwinCreate, GUI_COUNTOF(_aDialogRunwinCreate), _cbRunwinDialog, HDTWIN, 0, 0);
	runWin = hWin;
	char *mode[2] = {"命令模式","程序模式"};
    while(runWin_flag)
    {
        hItem = WM_GetDialogItem(hWin, ID_RTEXT_0);
		sprintf(textbuff, "绝对坐标(mm)\n X:%.1f Y:%.1f Z:%.1f\n相对坐标(mm)\n X:%.1f Y:%.1f Z:%.1f \n C:%d F:%.1f", \
							(float)MC_Execute_param.X_current/10.0,(float)MC_Execute_param.Y_current/10.0,(float)MC_Execute_param.Z_current/10.0,
							(float)MC_Execute_param.X_work_current/10.0,(float)MC_Execute_param.Y_work_current/10.0,(float)MC_Execute_param.Z_work_current/10.0, \
							100,(float)stepper_speed/10.0);
		TEXT_SetText(hItem,textbuff);
		//gc_execute(cicr_ex);
		//if(runM)
		if(open_edit_flag == 1)
		{
			char read_path[20] = {0};
			sprintf(read_path,"1:/user/%s",celfile_text_buffer);
			exe_text_res = f_open(&exe_text,read_path,FA_OPEN_EXISTING|FA_WRITE|FA_READ);
			if(exe_text_res == FR_OK)
			{
				memset(read_buffer,0,sizeof(read_buffer));
				exe_text_res = f_lseek(&exe_text,0);
				exe_text_res = f_read(&exe_text,read_buffer,f_size(&exe_text),&exe_text_fnum);
				if(exe_text_res == FR_OK)
				{
					//读取的代码在read_buffer中;
					printf("%s",read_buffer);	
					//memset(text_buff,0,sizeof(text_buff));
					hItem = WM_GetDialogItem(hWin, ID_RTEXT_4);	
					TEXT_SetText(hItem,strtok(read_buffer,"\n"));					
					for(uint8_t i = 0; i < 2; i ++)
					{
						//memset(text_buff,0,sizeof(text_buff));
						hItem = WM_GetDialogItem(hWin, ID_RTEXT_5 + i);
						TEXT_SetText(hItem,strtok(NULL,"\n"));	
					}
					IsOpenFile = 1;
				}
				//free(read_buffer);
				//sprintf(file_name_buff,"%s", celfile_text_buffer);
			}
			else
			{
				printf("file not existing\r\n");
				IsOpenFile = 0;
			}
			f_close(&exe_text);		
			open_edit_flag = 0;	
			executePragram = 0;			
		}

		if(executePragram == 1)
		{
			if(IsOpenFile == 1)
			{
				if(circular_para.motionstatus == 0 && interpolation_para.motionstatus == 0)
				{
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					TEXT_GetText(WM_GetDialogItem(hWin, ID_RTEXT_3),ex_text_buff,50);
					TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_2),ex_text_buff);
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					TEXT_GetText(WM_GetDialogItem(hWin, ID_RTEXT_4),ex_text_buff,50);
					TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_3),ex_text_buff);
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					TEXT_GetText(WM_GetDialogItem(hWin, ID_RTEXT_5),ex_text_buff,50);
					TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_4),ex_text_buff);
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					TEXT_GetText(WM_GetDialogItem(hWin, ID_RTEXT_6),ex_text_buff,50);
					TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_5),ex_text_buff);
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					
					char *temp;
					temp = strtok(NULL,"\n");
					//TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_6),strtok(NULL,"\n"));	
					if(temp == NULL)
					{
						TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_6),"");
					}
					else
					{
						TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_6),temp);	
					}
					
					memset(ex_text_buff,0,sizeof(ex_text_buff));
					TEXT_GetText(WM_GetDialogItem(hWin, ID_RTEXT_3),ex_text_buff,50);
					if(strcmp(ex_text_buff,"M2") == 0)
					{
						executePragram = 0;
					}
					else
					{
						gc_init();
						if(gc_execute_line(ex_text_buff) != 0)
						{
							TEXT_SetBkColor(WM_GetDialogItem(hWin, ID_RTEXT_3),GUI_RED);
							executePragram = 0;
						}
					}				
				}

			}
			else
			{
				//GUI_MessageBox("No open file","Error",0);
				printf("未打开文件");
			}	
		}
			
		TEXT_SetText(WM_GetDialogItem(hWin, ID_RTEXT_7),mode[runMode_flag]);
		GUI_Delay(30);
    }

    return hWin;
}


// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
